﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace ZLSoft.ZLDocEditorEvents
{
    internal class HttpFun
    {

        public static string httpPost(string strUrl, string postDataStr, Dictionary<string, string> dicHeader, string strMethod = "POST") {
            //发送
            System.GC.Collect();//系统回收垃圾
            if (strMethod == "GET") strUrl = strUrl + "?" + postDataStr;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
            request.Timeout = 1000 * 20;
            if (strMethod == "POST") {
                request.Method = "POST";
            } else {
                request.Method = "GET";
            }

            request.Proxy = null;
            request.AllowAutoRedirect = true;

            request.ContentType = "application/json";
            request.Accept = "application/json";

            if (dicHeader != null) {
                foreach (var item in dicHeader) {
                    request.Headers.Add(item.Key, item.Value);
                }
            }

            try {
                if (strMethod == "POST") {
                    byte[] payload = Encoding.UTF8.GetBytes(postDataStr);
                    request.ContentLength = payload.Length;
                    request.ServicePoint.Expect100Continue = false;
                    request.GetRequestStream().Write(payload, 0, payload.Length);
                }
                //响应接收
                HttpWebResponse response;
                try {
                    response = (HttpWebResponse)request.GetResponse();
                } catch (WebException ex) {
                    response = (HttpWebResponse)ex.Response;
                }

                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
                string retString = myStreamReader.ReadToEnd();

                response.Close();
                myStreamReader.Close();
                myResponseStream.Close();
                myResponseStream.Dispose();
                response = null;
                myStreamReader = null;
                myResponseStream = null;
                request.Abort();
                request = null;

                return retString;
            } catch (Exception ex) {
                request.Abort();
                request = null;
                Console.WriteLine(ex.Message);
                return "";
            }
        }
            /// <summary>
            /// 调用Http服务
            /// </summary>
            /// <param name="strUrl">Http地址</param>
            /// <param name="postDataStr">发送的数据</param>
            /// <param name="dicHeader">Http Header</param>
            /// <param name="strMethod">三方服务调用方法</param>
            /// <returns></returns>
            //public static string httpPost(string strUrl, string postDataStr, Dictionary<string, string> dicHeader, string strMethod = "POST")  //post读取
            //{
            //    //发送
            //    System.GC.Collect();//系统回收垃圾
            //    if (strMethod == "GET") strUrl = strUrl + "?" + postDataStr;
            //    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
            //    request.Timeout = 1000 * 20;
            //    if (strMethod == "POST")
            //    {
            //        request.Method = "POST";
            //    }
            //    else
            //    {
            //        request.Method = "GET";
            //    }

            //    request.Proxy = null;
            //    request.AllowAutoRedirect = true;

            //    request.ContentType = "application/json";
            //    request.Accept = "application/json";

            //    if (dicHeader != null)
            //    {
            //        foreach (var item in dicHeader)
            //        {
            //            request.Headers.Add(item.Key, item.Value);
            //        }
            //    }

            //    try
            //    {
            //        if (strMethod == "POST")
            //        {
            //            byte[] payload = Encoding.UTF8.GetBytes(postDataStr);
            //            request.ContentLength = payload.Length;
            //            request.ServicePoint.Expect100Continue = false;
            //            request.GetRequestStream().Write(payload, 0, payload.Length);
            //        }
            //        //响应接收
            //        HttpWebResponse response;
            //        try
            //        {
            //            response = (HttpWebResponse)request.GetResponse();
            //        }
            //        catch (WebException ex)
            //        {
            //            response = (HttpWebResponse)ex.Response;
            //        }
            //        Stream myResponseStream = response.GetResponseStream();
            //        StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
            //        string retString = myStreamReader.ReadToEnd();

            //        response.Close();
            //        myStreamReader.Close();
            //        myResponseStream.Close();
            //        myResponseStream.Dispose();
            //        response = null;
            //        myStreamReader = null;
            //        myResponseStream = null;
            //        request.Abort();
            //        request = null;

            //        return retString;
            //    }
            //    catch (Exception ex)
            //    {
            //        request.Abort();
            //        request = null;
            //        Console.WriteLine(ex.Message);
            //        return "";
            //    }
            //}


        }
}
